/**
 * Created by Administrator on 2017/8/22 0022.
 */
var Category;
if (!Category) {
    Category = {}
}

var num = 0;

paramstypeFormatter = function (value, row, index) {
    return [
        '<a class="edit" href="javascript:void(0)" style="color: #303051">编辑</a>'
    ].join('')
};

valueFormatter = function (value, row, index) {
    if (num == 0) {
        return [
            '<span >无值</span>'
        ].join('')
    } else {
        return [
            '<span >' + row.value + '</span>'
        ].join('')
    }
};


window.paramstypeEvent = {
    'click .edit': function (e, value, row, index) {
        if (num == 0) {
            showModel("editparamstype-div", function () {
                setRowToFrm("editparamstype", row);
                $("#remark").val(row.remark);
                $.each($("#editparamstype input"), function () {
                    var value = $(this).val();
                    $(this).val(value).css("color", "#000");
                });
                $.each($("#editparamstype label[class=error]"), function () {
                    $(this).remove();
                });
            })
        } else {
            showModel("editparams-div", function () {
                setRowToFrm("editparams", row);
                $("#id").val(row.id);
                $("#value").val(row.value);

                $.each($("#editparams input"), function () {
                    var value = $(this).val();
                    $(this).val(value).css("color", "#000");
                });

                $.each($("#editparams label[class=error]"), function () {
                    $(this).remove();
                });

                $("#editparams input[name=typeId]").val($("#typeId").val());

            })
        }
    },
    'click .delete': function (e, value, row, index) {
        var id = row.id;
        var typeId = $("#typeId").val();
        comment.showOperationTips("你确认要删除吗？", function () {
            $.post($().AbsUrlParse("/web/parameter/delete"), {
                id: id,
                typeId: typeId
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $('#table');
                        table.bootstrapTable('refresh');
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });


        })
    }
}

var clearPathClick = function (id, typeId) {
    if (id == 0) {
        if (num >= 1) {
            num -= 1;
        }
        $("#typeId").val(0);
        $("#table").bootstrapTable("hideColumn", "value");
    }

    queryCity(id);

    var spans = $('#path').children("span");
    var isStart = false;
    $.each(spans, function () {
        var s = $(this);
        if (isStart) {
            s.remove();
        } else {
            var mid = s.attr('data-id');
            if (mid == id) {
                isStart = true;
            }
        }
    });
}
Category.nameFormatters = function (value, row, index) {
    if (num == 0) {
        return '<span class="child line">' + row.name + '</span>';
    } else {
        return '<span>' + row.name + '</span>'
    }
};

/**
 * 所有类别操作按钮
 * @type {{click .child: Function, click .childs: Function, click .edit: Function, click .hot: Function, click .remove: Function}}
 */


var pathClicks = function (id, typeId, name) {
    num += 1;
    queryCity(id);
    $("#table").bootstrapTable("showColumn", "value");
    // $("#table").bootstrapTable("showColumn", "remark");
    // $("#table").bootstrapTable("hideColumn", "descri");
    $("#typeId").val(id);
    $('#path').append('<span class="path" data-id="' + id + '" onclick="clearPathClick(' + id + ',' + typeId + ')">' + name + '>&nbsp;&nbsp;</span>');
};


var queryCity = function (id) {
    if (id == 0) {
        var table = $("#table");
        var url = $().AbsUrlParse('/web/parameter/paramsType');
        table.bootstrapTable("refresh", {url: url});
    } else {
        var table = $("#table");
        var url = $().AbsUrlParse('/web/parameter/paramsType?typeId=' + id + "&offset=0");
        table.bootstrapTable("refresh", {url: url});
    }
};

window.operateEvents = {
    'click .line': function (e, value, row, index) {
        $("#typeId").val(row.type_id);
        pathClicks(row.id, row.type_id, row.name);
    },

    'click .edit': function (e, value, row, index) {
        showEditGrade(row);
    },

    'click .remove': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/parameter/delete?'), {
                id: row.id, typeId: row.typeId
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .status': function (e, value, row, index) {
        showEditGrade(row);
    }
}
/**
 * 添加
 */
bindClick("#addWear", function () {
    var typeId = $("#typeId").val();
    showAddCategory(typeId);
});

/**
 * 弹出添加框
 */
var showAddCategory = function (typeId) {
    if (num == 0) {
        showModel("addParameterType-div", function () {
            $("input[name=typeId]").val(typeId);
            $("#addParameterType").resetForm();

        });
    } else {
        showModel("addParameter-div", function () {
            $("input[name=typeId]").val(typeId);
            $("#addParameter").resetForm();

        });
    }
}

/**
 * 确认添加按钮事件
 */
bindClick("button[data-event=submit-addParameterTypeSave]", function (tag, typeId) {

    var form = $("#addParameterType");
    var v = form.valid();
    var a = $("#parameterTypeName").val();
    var b = $("#parameterTypeDesc").val();


    if (a == "" || a.trim() == "") {
        hint("参数名称没有填写");
        return false;
    }
    if (b == "" || b.trim() == "") {
        hint("参数描述没有填写");
        return false;
    }

    var param = form.formToArray();
    $.post(form.attr("action"), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功！');
            closeModel('addParameterType-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

/**
 * 确认添加按钮事件
 */
bindClick("button[data-event=submit-addParameterSave]", function (tag, typeId) {
    var typeId = $("#typeid2").val($("#typeId").val());
    var form = $("#addParameter");

    var a = $("#addParameterName").val();
    var b = $("#addParameterValue").val()
    var c = $("#addParameterRemark").val();

    if (a == "" || a.trim() == "") {
        hint("参数名称没有填写");
        return flase;
    }

    if (b == "" || b.trim() == "") {
        hint("参数值没有填写");
        return false;
    }

    if (c == "" || c.trim() == "") {
        hint("介绍没有填写");
        return false;
    }

    var param = form.formToArray();
    $.post(form.attr("action"), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功！');
            //关闭model方法
            closeModel('addParameter-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

bindClick("#btnsearch", function () {
    var paramsName = $("#paramsName").val();
    var typeId = $("#typeId").val();
    var url = "";
    if (num == 0) {
        url = $().AbsUrlParse("/web/parameter/paramsType?paramsName=" + paramsName + "&num=" + num);
        $("#table").bootstrapTable("refresh", {url: url});
    } else {
        url = $().AbsUrlParse("/web/parameter/paramsType?paramsName=" + paramsName + "&typeId=" + typeId + "&num=" + num);
        $("#table").bootstrapTable("refresh", {url: url});
    }
});


/**
 * 确认修改按钮事件
 */
bindClick("#saveeditparamstype", function () {
    var form = $("#editparamstype");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var param = form.formToArray();
    $.post(form.attr("action"), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            closeModel('editparamstype-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});
/**
 * 确认修改按钮事件
 */
bindClick("#saveeditparams", function () {
    var form = $("#editparams");

    var v = form.valid();
    if (v == false) {
        return false;
    }

    var param = form.formToArray();
    $.post(form.attr("action"), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            closeModel('editparams-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

$(function () {
    $("#typeId").val(0);
});

